package cmw.com.beijingnew.pager;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apkfuns.logutils.LogUtils;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cmw.com.base.BasePager;
import cmw.com.base.MenuDetailBasePager;
import cmw.com.beijingnew.activity.MainActivity;
import cmw.com.beijingnew.constant.SysConstant;
import cmw.com.beijingnew.fragment.LeftmenuFragment;
import cmw.com.beijingnew.menudetailpager.InteracMenuDetailPager;
import cmw.com.beijingnew.menudetailpager.NewsMenuDetailPager;
import cmw.com.beijingnew.menudetailpager.PhotosMenuDetailPager;
import cmw.com.beijingnew.menudetailpager.TopicMenuDetailPager;
import cmw.com.utils.CacheUtil;
import cmw.com.utils.JsonUtil;

/**
 * 新闻中心页面
 * Created by Administrator on 2017/4/8.
 */

public class NewsCenterPager extends BasePager {
    List<MenuDetailBasePager> detailBasePagers = new ArrayList<MenuDetailBasePager>();

    public NewsCenterPager(Context context) {
        super(context);
    }

    @Override
    public void initData() {
        LogUtils.e("新闻中心数据初始化...");

        ib_menu.setVisibility(View.VISIBLE);
        tv_title.setText("新闻中心");

        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(24);
        textView.setTextColor(Color.RED);
        fl_content.addView(textView);

        textView.setText("新闻中心内容");

        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        final String url = SysConstant.NEW_CHANNEL_URL;
        JSONObject jsonCacheData = CacheUtil.getJson(context, url);
        if(null != jsonCacheData){//先从缓存中加载数据
            process(jsonCacheData);
        }
        RequestParams entity = new RequestParams(url);
        x.http().get(entity, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.e("请求成功，result="+result);
                Object json = JSON.parse(result);
                LogUtils.e(json);
                JSONObject jsonObj = JsonUtil.convertStrToJSONObj(result);
                CacheUtil.putJson(context, url, jsonObj);
                process(jsonObj);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.e("请求失败，result="+ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
                LogUtil.e("请求取消，result="+cex.getMessage());
            }

            @Override
            public void onFinished() {
                LogUtil.e("请求完成。。。");
            }
        });
    }

    private void process(JSONObject jsonObj){
        MainActivity mainActivity = (MainActivity)context;
        LeftmenuFragment leftmenuFragment = mainActivity.getLeftmenuFragment();
        JSONArray jsonArr = jsonObj.getJSONArray("result");
        detailBasePagers.add(new NewsMenuDetailPager(context, jsonArr));
        detailBasePagers.add(new TopicMenuDetailPager(context, jsonArr));
        detailBasePagers.add(new PhotosMenuDetailPager(context));
        detailBasePagers.add(new InteracMenuDetailPager(context));
        JSONArray leftMenus = getLeftMenus();
        leftmenuFragment.setData(leftMenus);
    }

    /**
     * 获取新闻中心左边菜单
     * @return
     */
    private JSONArray getLeftMenus(){
        List<String> list = new ArrayList<>();
        list.add("新闻");
        list.add("专题");
        list.add("组图");
        list.add("互动");
        list.add("投票");
        JSONArray arr = new JSONArray();
        arr.addAll(list);
        return arr;
    }

    public void switchPager(int i) {
        fl_content.removeAllViews();
        MenuDetailBasePager menuDetailBasePager = detailBasePagers.get(i);
        View rootView = menuDetailBasePager.rootView;
        menuDetailBasePager.initData();
        fl_content.addView(rootView);
    }
}
